// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._time = 0;
        this.sp = this.node.getComponent(cc.Sprite);

        let size = this.node.getContentSize();

        // this.node.y = -cc.winSize.height/2 + size.height/2 + 100;
        // this.node.x = 0; //size.width/2 - cc.winSize.width/2;
    },

    start () {
        cc.log('shaderTime start called!');
        let size = this.node.getContentSize();
        this.material = this.sp.getMaterial(0);
        cc.log('size:',size.width,size.height);
        this.material.effect.setProperty('u_resolution',{
            x:size.width,
            y:size.height
        });

        this.material.effect.setProperty('u_nodesize',{
            x:size.width,
            y:size.height
        });

        let worldPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());

        this.material.effect.setProperty('u_left_bottom',{
            x:worldPos.x - size.width/2,
            y:worldPos.y - size.height/2
        });
    },

    update (dt) {
        if(this.node.active && this.material){
            this._setShaderTime(dt);
        }
    },

    _setShaderTime(dt){
        this._time += dt;
        this.material.effect.setProperty('u_time',this._time);
    },

});
